<template>
  <a-modal
    title="新增食谱"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :labelCol="{'width':'40%'}"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model
        ref="form"
        :model="form"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
        :rules="rules"
      >
        <a-row justify="start">
          <a-col span="10">
            <a-form-model-item
              label="菜谱名称"
              prop="recipeName"
            >
              <a-input
                v-model="form.recipeName"
                style="width: 50%"
                placeholder=""
              />
            </a-form-model-item>
          </a-col>
          <a-col span="10">
            <a-form-model-item
              label="简写码"
              prop="abbreviation"
            >
              <a-input
                v-model="form.abbreviation"
                style="width: 50%"
                placeholder=""
              />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-button
          class="editable-add-btn"
          @click="handleAdd(form.id)"
        >
          新增
        </a-button>
        <a-form-model-item
          prop="recipesDetailsList"
          :wrapperCol="{width:'100%;'}"
        >
          <a-table
            :dataSource="form.recipesDetailsList"
            :columns="columns"
            :rowKey="(record) => record.foodMaterialsId"
            style="width: 100%"
          >
            <template
              slot="foodName"
              slot-scope="text, record"
            >
              <a-input
                v-model="record.foodName"
                @click="current=record.foodMaterialsId"
                @dblclick="current=undefined"
                placeholder="请单点选择食物名称"
                style="width: 500px;padding: 0;border: 0"
                allowClear
              />
              <SelectTable
                v-show="current===record.foodMaterialsId"
                :queryText="record.foodName"
                @update:foodMaterialsId="onCellChange(record.foodMaterialsId, arguments)"
              />
            </template>
            <template
              slot="weight"
              slot-scope="text, record"
            >
              <a-input-number
                v-model="record.weight"
                :step="0.1"
                style="width: 50%;"
              />
            </template>
            <template
              slot="operation"
              slot-scope="text, record"
            >
              <a-popconfirm
                title="确认删除?"
                @confirm="() => onDelete(record.foodMaterialsId)"
              >
                <a href="javascript:;">移除</a>
              </a-popconfirm>
            </template>
          </a-table>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { addCookBookStore } from "@/api/modular/cookBook/cookBookManage";
import { getUuid } from "@/utils/util";
import SelectTable from "@/views/cookbook/SelectTable";

export default {
  components: { SelectTable },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 18 }
      },
      form: {
        id: undefined,
        recipeName: undefined,
        abbreviation: undefined,
        recipesDetailsList: []
      },
      columns: [
        {
          title: "食物名称",
          scopedSlots: { customRender: "foodName" }
        },
        {
          title: "食物数量(克)",
          scopedSlots: { customRender: "weight" },
          width: 200
        },
        {
          title: "操作",
          scopedSlots: { customRender: "operation" }
        }
      ],
      rules: {
        recipeName: [{ required: true, message: "请输入菜谱名称！", trigger: "blur" }],
        abbreviation: [{ required: true, message: "请输入简写码！", trigger: "blur" }]
      },
      // selectTable坐标
      current: undefined,
      confirmLoading: false,
      visible: false
    };
  },
  methods: {
    add() {
      this.visible = true;
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.confirmLoading = true;
          const id = getUuid();
          this.form.id = id;
          this.form.recipesDetailsList.forEach(i => i.recipesId = id);
          addCookBookStore(this.form).then((res) => {
            if (res.success) {
              this.$message.success("新增成功");
              this.visible = false;
              this.$emit("ok");
              this.$refs.form.resetFields();
            } else {
              this.$message.error("新增失败：" + res.message);
            }
          }).finally((res) => {

            this.confirmLoading = false;
          });
        } else {
          return false;
        }
      });
    },
    handleCancel() {
      this.$refs.form.resetFields();
      this.visible = false;
    },
    onCellChange(rawId, args) {
      if (this.form.recipesDetailsList.find(i => i.foodMaterialsId === args[0])) return this.$message.warning("食谱中已存在该食物！");
      const curRecipesDetailsList = this.form.recipesDetailsList.find(i => i.foodMaterialsId === rawId);
      curRecipesDetailsList.foodMaterialsId = args[0];
      curRecipesDetailsList.foodName = args[1];
      this.current = undefined;
    },
    handleAdd(recipesId) {
      this.current = "";
      this.form.recipesDetailsList.push({
        weight: undefined,
        foodMaterialsId: undefined,
        foodName: undefined,
        recipesId
      });
    },
    onDelete(id) {
      this.form.recipesDetailsList = this.form.recipesDetailsList.filter(item => item.foodMaterialsId !== id);
    }
  }
};
</script>

<style lang="less" scoped>
//::v-deep ant-form-item-control-wrapper {
//  width: 100% !important;
//}

.unit {
  margin-left: 5px;
}

.editable-add-btn {
  margin-bottom: 8px;
}


</style>
